#!/bin/sh

ORIG_NVM_DIR="${NVM_DIR-}"
TEST_DIR=''

cleanup () {
  unset -f nvm_print_implicit_alias
  case "${TEST_DIR}" in
    /*) rm -rf "${TEST_DIR}" ;;
  esac
  NVM_DIR="${ORIG_NVM_DIR}"
  export NVM_DIR
}

die () { echo "$@" ; cleanup ; exit 1; }

: nvm.sh
\. ../../../nvm.sh

\. ../../common.sh

TEST_DIR="$(mktemp -d)" || die 'could not create a temp dir'
NVM_DIR="${TEST_DIR}/nvm"
export NVM_DIR
ALIAS_DIR="${NVM_DIR}/alias"
mkdir -p "${ALIAS_DIR}" || die 'could not create the alias dir'

IMPLICIT_ARGS_FILE="${TEST_DIR}/implicit-args"
IMPLICIT_RESULT=''
nvm_print_implicit_alias() {
  printf '%s|' "$@" >> "${IMPLICIT_ARGS_FILE}"
  echo 'implicit noise' >&2
  if [ -n "${IMPLICIT_RESULT}" ]; then
    echo "${IMPLICIT_RESULT}"
  fi
}

make_alias () {
  printf '%s\n' "$2" > "${ALIAS_DIR}/$1" || die "could not write alias $1"
}

expect () {
  try nvm_resolve_alias "$1"
  [ "_${CAPTURED_STDOUT}" = "_$2" ] || die "'nvm_resolve_alias $1': got >${CAPTURED_STDOUT}<, expected >$2<"
  [ "_${CAPTURED_EXIT_CODE}" = "_$3" ] || die "'nvm_resolve_alias $1': exit code >${CAPTURED_EXIT_CODE}<, expected >$3<"
}

# missing or empty argument
try nvm_resolve_alias
[ "_${CAPTURED_EXIT_CODE}" = '_1' ] || die "no argument: exit code >${CAPTURED_EXIT_CODE}<, expected >1<"
[ -z "${CAPTURED_STDOUT}" ] || die "no argument: got stdout >${CAPTURED_STDOUT}<"
expect '' '' 1

# a nonexistent, non-implicit alias: status 2, no output, nvm_alias's error silenced
expect nonexistent '' 2
try_err nvm_resolve_alias nonexistent
[ -z "${CAPTURED_STDERR}" ] || die "nonexistent alias: got stderr >${CAPTURED_STDERR}<"
[ ! -f "${IMPLICIT_ARGS_FILE}" ] || die "a non-implicit alias should not consult nvm_print_implicit_alias; got >$(cat "${IMPLICIT_ARGS_FILE}")<"

# a traversal name is rejected by nvm_alias, so it resolves to nothing
printf '%s\n' 'v6.6.6' > "${TEST_DIR}/outside"
expect '../../outside' '' 2

# version targets get a `v` prefix
make_alias bare '1.2.3'
expect bare 'v1.2.3' 0
make_alias prefixed 'v4.5.6'
expect prefixed 'v4.5.6' 0
make_alias partial '18'
expect partial 'v18' 0

# only the first line of a multi-line alias file is followed
printf '%s\n' '7.8.9' 'v1.1.1' 'v2.2.2' > "${ALIAS_DIR}/multi" || die 'could not write alias multi'
expect multi 'v7.8.9' 0

# chains are followed to their end
make_alias chain1 'chain2'
make_alias chain2 'chain3'
make_alias chain3 '10.0.0'
expect chain1 'v10.0.0' 0
expect chain2 'v10.0.0' 0

# a target that is not itself an alias ends the chain unchanged apart from the prefix
make_alias words 'not-a-version'
expect words 'not-a-version' 0

# the node and iojs prefixes are printed as-is, without a `v`
make_alias to-node 'node'
expect to-node 'node' 0
make_alias to-iojs 'iojs'
expect to-iojs 'iojs' 0
make_alias to-iojs-dash 'iojs-'
expect to-iojs-dash 'iojs-' 0
make_alias to-iojs-version 'iojs-1.0.0'
expect to-iojs-version 'iojs-v1.0.0' 0
make_alias to-iojs-v 'iojs-v3.3.1'
expect to-iojs-v 'iojs-v3.3.1' 0

# cycles print the infinity sign and succeed
make_alias self 'self'
expect self '∞' 0
make_alias loop1 'loop2'
make_alias loop2 'loop3'
make_alias loop3 'loop1'
expect loop1 '∞' 0
make_alias into-loop 'loop2'
expect into-loop '∞' 0

# names are compared literally: no glob or word splitting
make_alias 'ab' 'a*'
make_alias 'a*' '2.0.0'
expect ab 'v2.0.0' 0
make_alias 'foo bar' 'midway'
make_alias midway 'bar'
make_alias bar '0.0.99'
expect 'foo bar' 'v0.0.99' 0
make_alias 'x y' 'x'
make_alias x '3.0.0'
expect 'x y' 'v3.0.0' 0

# leading `--` words are flags, kept in front of the final target
make_alias flagged '--lts 20.1.0'
expect flagged '--lts v20.1.0' 0
make_alias two-flags '--lts --foo 20.1.0'
expect two-flags '--lts --foo v20.1.0' 0
make_alias flagged-node '--lts node'
expect flagged-node '--lts node' 0
make_alias flagged-iojs '--lts iojs'
expect flagged-iojs '--lts iojs' 0

# a single dash does not make a flag
make_alias single-dash '-x 1.0.0'
expect single-dash '-x 1.0.0' 0

# a flag with no target is itself the target
make_alias flag-only '--lts'
expect flag-only '--lts' 0

# the outermost alias's flags apply
make_alias outer-flags '--outer flagged'
expect outer-flags '--outer v20.1.0' 0
make_alias outer-plain 'flagged'
expect outer-plain '--lts v20.1.0' 0
make_alias plain-to-plain 'outer-plain'
expect plain-to-plain '--lts v20.1.0' 0

# a flag pointing at another alias is followed
make_alias flag-chain '--lts chain1'
expect flag-chain '--lts v10.0.0' 0

# flags do not decorate a cycle
make_alias flag-loop '--lts loop1'
expect flag-loop '∞' 0

# the function's locals do not leak into the caller
NVM_ALIAS='caller'
NVM_PATTERN='caller'
NVM_ALIAS_FLAGS='caller'
NVM_SEEN_ALIASES='caller'
NVM_ALIAS_TEMP='caller'
NVM_ALIAS_OUTPUT='caller'
NVM_ALIAS_TEMP_FLAGS='caller'
NVM_IOJS_PREFIX='caller'
NVM_NODE_PREFIX='caller'
nvm_resolve_alias flagged > /dev/null
for NAME in NVM_ALIAS NVM_PATTERN NVM_ALIAS_FLAGS NVM_SEEN_ALIASES NVM_ALIAS_TEMP NVM_ALIAS_OUTPUT NVM_ALIAS_TEMP_FLAGS NVM_IOJS_PREFIX NVM_NODE_PREFIX; do
  eval "VALUE=\"\${${NAME}}\""
  [ "_${VALUE}" = '_caller' ] || die "${NAME} leaked: >${VALUE}<"
done
NVM_IMPLICIT='caller'
IMPLICIT_RESULT='0.10'
nvm_resolve_alias unstable > /dev/null
[ "_${NVM_IMPLICIT}" = '_caller' ] || die "NVM_IMPLICIT leaked: >${NVM_IMPLICIT}<"
rm -f "${IMPLICIT_ARGS_FILE}"

# implicit aliases with no alias file fall back to nvm_print_implicit_alias, still with status 2
IMPLICIT_RESULT='0.10'
expect stable 'v0.10' 2
[ "_$(cat "${IMPLICIT_ARGS_FILE}")" = '_local|stable|' ] || die "nvm_print_implicit_alias got args >$(cat "${IMPLICIT_ARGS_FILE}")<, expected >local|stable|<"
try_err nvm_resolve_alias stable
[ -z "${CAPTURED_STDERR}" ] || die "implicit alias: got stderr >${CAPTURED_STDERR}<"
IMPLICIT_RESULT='iojs-1.0'
expect iojs 'iojs-v1.0' 2
IMPLICIT_RESULT='v0.11'
expect unstable 'v0.11' 2
expect node 'v0.11' 2

# an implicit alias with nothing installed prints nothing
IMPLICIT_RESULT=''
expect stable '' 2
OUTPUT="$(nvm_resolve_alias stable 2>/dev/null; echo x)"
[ "_${OUTPUT}" = '_x' ] || die "implicit alias with nothing installed should print nothing at all; got >${OUTPUT}<"

# an alias file overrides the implicit lookup
rm -f "${IMPLICIT_ARGS_FILE}"
IMPLICIT_RESULT='0.10'
make_alias stable '5.0.0'
expect stable 'v5.0.0' 0
[ ! -f "${IMPLICIT_ARGS_FILE}" ] || die "an existing alias file should not consult nvm_print_implicit_alias"

cleanup
